package com.yuelan.codelib.utils;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

public class ViewUtil {
	/**
	 * 作者: peijiangping<BR>
	 * 时间:2012-12-27下午1:36:38<BR>
	 * 功能:动态设置listview高度<BR>
	 * 返回值:void<BR>
	 */
	public static void setListViewHeightBasedOnChildren(ListView listView,
			int value) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			return;
		}
		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}
		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight
				+ (listView.getDividerHeight() * (listAdapter.getCount() - 1))
				+ value;
	}
}
